package com.zhuozhou.jdyc.main.main.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.net.http.SslError;
import android.os.Bundle;
import android.view.View;
import android.webkit.SslErrorHandler;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.zhuozhou.jdyc.R;
import com.zhuozhou.jdyc.base.BaseActivity;
import com.zhuozhou.jdyc.databinding.BaseWebviewBinding;
import com.zhuozhou.jdyc.main.main.viewmodel.MainViewModel;
import com.zhuozhou.jdyc.util.apiUtil.DataHolder;

/**
 * Created by 方舟 on 2017/11/28.
 * 通知公告
 */

public class InformWebViewActivity extends BaseActivity<MainViewModel, BaseWebviewBinding> {

    private static final int BANK_RESULT = 0;
    private String content, title, time;
    public static int content_id = 31000;//内容id

    @Override
    protected int getLayoutId() {
        return R.layout.base_webview;
    }

    @Override
    public String setTitleBar() {
        return "活动简介";
    }


    public static void show(Activity context, Bundle bundle) {
        Intent intent = new Intent(context, InformWebViewActivity.class);
        intent.putExtras(bundle);
        context.startActivityForResult(intent, BANK_RESULT);
    }

    public static void show(Context context, Bundle bundle) {
        Intent intent = new Intent(context, InformWebViewActivity.class);
        intent.putExtras(bundle);
        context.startActivity(intent);
    }

    private void initWebView() {
        binding.webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                if (url.contains(".pdf") || url.contains(".zip") || url.contains(".xls") || url.contains(".doc")) {
                    Intent intent = new Intent();
                    intent.setAction("android.intent.action.VIEW");
                    Uri uri = Uri.parse(url);
                    intent.setData(uri);
                    startActivity(intent);//打开浏览器
                    return true;
                } else {
                    view.loadUrl(url); // 在当前的webview中跳转到新的url
                    return false;
                }
            }

            public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
                handler.proceed(); // 接受所有网站的证书
            }
        });

        WebSettings settings = binding.webView.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setUseWideViewPort(true);
        settings.setCacheMode(WebSettings.LOAD_NO_CACHE);
        binding.webView.setWebChromeClient(new MyWebChromeClient());
        binding.webView.loadUrl("file:///android_asset/tongzhi.html");
    }

    //Web视图
    private class MyWebChromeClient extends WebChromeClient {
        @Override
        public void onProgressChanged(WebView view, int newProgress) {
            if (newProgress == 100) {
                binding.progressBar.setVisibility(View.GONE);//开始加载网页时显示进度条
                binding.webView.loadUrl("javascript:setTitle(" + "'" + title + "'" + ")");
                // binding.webView.loadUrl("javascript:setDate(" + "'" + ("发布于：" + time) + "'" + ")");
                binding.webView.loadUrl("javascript:setContents(" + "'" + content + "'" + ")");
            } else {
                binding.progressBar.setVisibility(View.VISIBLE);//开始加载网页时显示进度条
                binding.progressBar.setProgress(newProgress);//设置进度值
            }
        }
    }

    @Override
    public void initView() {
        initWebView();
        Bundle bundle = getIntent().getExtras() == null ? new Bundle() : getIntent().getExtras();
        content_id = bundle.getInt("contentId", content_id);//通过id去寻找数据，防止数据过长的问题
        title = bundle.getString("title", "");
        //  time = bundle.getString("AddTime", "");
        content = DataHolder.getInstance().retrieve(content_id + "") == null ? "" : DataHolder.getInstance().retrieve(content_id + "").toString();
    }

    @Override
    public void initData(Bundle bundle) {

    }


}
